package xyf.p11.other;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * ClassName: M1615_MasterMind
 * Description:
 * Author: xyf
 * Date: 9/17/22 8:02 AM
 * Version: 1.0
 **/
public class M1615_MasterMind
{
    public int[] masterMind(String solution, String guess)
    {
        Map<Character, Integer> solutionMap = new HashMap<>();
        Map<Character, Integer> guessMap = new HashMap<>();

        int guessRight = 0;
        for (int i = 0; i < solution.length(); i++)
        {
            Integer solutionC = solutionMap.get(solution.charAt(i));
            if (null == solutionC)
            {
                solutionC = 0;
            }
            solutionMap.put(solution.charAt(i), solutionC + 1);

            Integer guessC = guessMap.get(guess.charAt(i));
            if (null == guessC)
            {
                guessC = 0;
            }
            guessMap.put(guess.charAt(i), guessC + 1);

            if (solution.charAt(i) == guess.charAt(i))
            {
                guessRight += 1;
            }
        }

        Set<Map.Entry<Character, Integer>> entries = solutionMap.entrySet();
        int guessRight2 = 0;
        for (Map.Entry<Character, Integer> entry : entries)
        {
            Character key = entry.getKey();
            if (guessMap.get(key) != null)
            {
                guessRight2 += Math.min(entry.getValue(), guessMap.get(key));
            }
        }
        int[] res = new int[2];
        res[0] = guessRight;
        res[1] = guessRight2 - guessRight;
        return res;
    }
}